package simple;

import com.rabbitmq.client.*;
import config.RabbitConfig;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author tuyou_cd
 * @date 2020/12/10 16:27
 */
public class RabbitProducer {

    public static void main(String[] args) throws IOException, TimeoutException {

        try (Connection connection = RabbitConfig.getConnection();
             Channel channel = connection.createChannel()) {
            //定义一个交换器
            channel.exchangeDeclare("exchange_demo", "direct", true, false, null);
            //定义一个队列
            channel.queueDeclare("queue_demo", true, false, false, null);
            //把队列绑定到交换器上面，并指定绑定键名字
            channel.queueBind("queue_demo", "exchange_demo", "routingkey_demo");

            String message = "hello world!";
            //发送消息到指定的交换器并指定路由键名字
            channel.basicPublish("exchange_demo", "routingkey_demo", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        }
    }
}
